//
// FileName LocalDB
// ProjectName vue-three-admin
// ComputerUser gys
// Author 区区电脑
// Email 1573580882@qq.com
// Date 2021/5/7 10:45 上午
// Copyright © 2021 gys. All rights reserved.
import dayjs from "dayjs"

export class LocalDBItem {
    constructor({key, data}) {
        this.key = key;
        this.data = data;
        this.time = dayjs().format("YYYY-MM-DD HH:mm:ss:SSS");
    }
}

export default class LocalDB {
    static setItem(key, data) {
        if (!key) return console.error("LocalDB.setItem key 为毕传参数");
        const localData = JSON.stringify(new LocalDBItem({
            key,
            data
        }));
        window.localStorage.setItem(key, localData);
    }
    static getItem(key) {
        if (!key) return console.error("LocalDB.getItem key 为毕传参数");
        const localDataString = window.localStorage.getItem(key);
        if (localDataString) {
            return JSON.parse(localDataString)
        }
        return new LocalDBItem({
            key,
            data: null
        })
    }
    static removeItem(key) {
        if (!key) return console.error("LocalDB.removeItem key 为毕传参数");
        window.localStorage.removeItem(key);
    }

    static setSessionItem(key, data) {
        if (!key) return console.error("LocalDB.setSessionItem key 为毕传参数");
        const localData = JSON.stringify(new LocalDBItem({
            key,
            data
        }));
        window.sessionStorage.setItem(key, localData);
    }
    static getSessionItem(key) {
        if (!key) return console.error("LocalDB.getSessionItem key 为毕传参数");
        const localDataString = window.sessionStorage.getItem(key);
        if (localDataString) {
            return JSON.parse(localDataString)
        }
        return new LocalDBItem({
            key,
            data: null
        })
    }
    static removeSessionItem(key) {
        if (!key) return console.error("LocalDB.removeSessionItem key 为毕传参数");
        window.sessionStorage.removeItem(key);
    }
}
